package com.jtech.together.view.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.github.chrisbanes.photoview.OnOutsidePhotoTapListener;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.jtech.together.R;
import com.jtech.together.mvp.contract.PhotoGalleryContract;
import com.jtech.together.mvp.presenter.PhotoGalleryPresenter;
import com.jtech.together.util.ImageUtils;
import com.jtech.together.view.activity.base.BaseActivity;
import com.jtech.together.view.adapter.PhotoGalleryPagerAdapter;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 图片浏览
 * Created by wuxubaiyang on 2017/5/11.
 */
public class PhotoGalleryActivity extends BaseActivity implements PhotoGalleryContract.View {
    public static final String KEY_PHOTO_LIST = "keyPhotoList";
    public static final String KEY_PHOTO_INDEX = "keyPhotoIndex";

    @Bind(R.id.viewpager_photo_gallery)
    ViewPager viewPager;

    private PhotoGalleryContract.Presenter presenter;

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new PhotoGalleryPresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_photo_gallery);
        //设置viewpager适配器
        viewPager.setAdapter(new PhotoGalleryPagerAdapter(getPhotoViewList()));
    }

    @Override
    protected void loadData() {
        //设置默认显示
        viewPager.setCurrentItem(presenter.getDefaultIndex());
    }

    /**
     * 获取图片视图集合
     *
     * @return
     */
    private List<PhotoView> getPhotoViewList() {
        List<PhotoView> photoViews = new ArrayList<>();
        for (String imageUrl : presenter.getPhotoList()) {
            PhotoView photoView = new PhotoView(getActivity());
            //显示图片
            ImageUtils.showImage(getActivity(), imageUrl, photoView);
            //设置点击事件
            photoView.setOnOutsidePhotoTapListener(new OnOutsidePhotoTapListener() {
                @Override
                public void onOutsidePhotoTap(ImageView imageView) {
                    onBackPressed();
                }
            });
            photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
                @Override
                public void onPhotoTap(ImageView view, float x, float y) {
                    onBackPressed();
                }
            });
            //添加视图到集合
            photoViews.add(photoView);
        }
        return photoViews;
    }

    /**
     * 关闭按钮点击事件
     */
    @OnClick(R.id.imagebutton_photo_gallery)
    public void onCloseClick() {
        onBackPressed();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
        MobclickAgent.onPause(this);
    }
}